package com.lky.marshallmall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by
 * autor : Marshall lky
 * datetime: 2021/6/4 15:37
 */
@Configuration
public class MyRabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
//        String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("stock-event-exchange", true, false);
        return topicExchange;
    }
    @Bean
    public Queue stockReleaseQueue(){
//        String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> argument
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        return queue;
    }
    @Bean
    public Queue stockDelayQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",60000);
        Queue queue = new Queue("stock.delay.queue", true, false, false, arguments);
        return queue;
    }
    @Bean
    public Binding stockLockedBinding(){
//        String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        Binding binding = new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#", null);
        return binding;
    }
    @Bean
    public Binding stockReleaseBinding(){
//        String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        Binding binding = new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked", null);
        return binding;
    }

    @RabbitListener(queues = "stock.release.stock.queue")
        public void handle(Message message){
        System.out.println(message);
    }

//    @PostConstruct  //@PostConstruct：MyRabbitConfig对象创建完成以后调用该方法
//    public void initRabbitTemplate(){
//        //设置确认回调
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            /**
//             *
//             * @param correlationData 当前消息的唯一关联数据（消息的唯一ID）
//             * @param ack 消息是否成功收到
//             * @param cause 失败的原因
//             */
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("confirm... correlationData:["+correlationData+"],ack:["+ack+"],cause:["++"]");
//            }
//        });
//        //设置消息抵达队列的确认回调
//        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//            /**
//             * 只要消息没有投递给指定的队列，就触发该回调
//             * @param message 投递失败的消息详细信息
//             * @param replyCode 回复的状态码
//             * @param replyText 回复的文本内容
//             * @param exchange 当时这个消息发送给哪个交换机
//             * @param routingKey 当时消息发送指定哪个路由件
//             */
//            @Override
//            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//                System.out.println("Fail... Message:["+message+"],replyCode:["+replyCode+"],replyText:["+replyText+"],exchange:["+exchange+"],routingKey:["+routingKey+"]");
//            }
//        });
//    }

}
